import requests
import re
import os
#此方案已废弃，因为只能爬到small版本的图片。最新的见_6_realNetbian.py
#修改自_4_jj20.py
#2021-0420 09:15
url="http://www.netbian.com"
headers={"User-Agent":"Mozilla/5.0 (X11; CrOS armv7l 13597.84.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.187 Safari/537.36",
         "referer":"http://www.netbian.com/",
         "connection":"keep-alive"}
urlFile="_6_netbian.txt"
#以GET的方式获取请求。
r=requests.get(url,headers=headers)
f=open(urlFile,"wb")
#r.content获取响应内容（字节流）
f.write(r.content)
f.close()

pttn="\S*.jpg"
f=open(urlFile,"r",encoding="gbk")
txt=f.read()
print(txt)
pngResult=re.findall(pttn,txt)#这个re出来的东西本身就是个List
f.close()
print('pngResult=',pngResult,'\n','pngResult个数为：',len(pngResult),'\n',str(pngResult))

pttn2="(https?\:\/\/img.netbian.com\/file\S*.jpg)"
httpsResult=re.findall(pttn2,str(pngResult))
print(httpsResult,'\n')

for i in range(len(httpsResult)):
    #print(f"pic{i} is ok.",'\n')
    url=httpsResult[i]
    urlFile2=f"/home/pi/Python/实战python爬虫/aboutUrllib/_6_pic/_6_netbian{i}.txt"
    #以GET的方式获取请求。
    r=requests.get(url,headers=headers)
    f=open(urlFile2,"wb")
    #r.content获取响应内容（字节流）
    f.write(r.content)
    f.close()

    pttn3="\S*.jpg"
    f=open(urlFile2,"r",encoding="gbk")
    txt=f.read()
    print(txt)
    pngResult2=re.findall(pttn3,txt)#这个re出来的东西本身就是个List
    f.close()
    print('pngResult=',pngResult2,'\n','pngResult个数为：',len(pngResult2),'\n',str(pngResult2))

    pttn4="(https?\:\/\/img.netbian.com\/file\S*.jpg)"
    httpsResult2=re.findall(pttn4,str(pngResult2))
    print(httpsResult2,'\n')

    r=requests.get(url)
    os.mkdir(f"/home/pi/Python/实战python爬虫/aboutUrllib/_6_pic/picDir{i}")
    for j in range(len(httpsResult2)):
        f=open(f"/home/pi/Python/实战python爬虫/aboutUrllib/_6_pic/picDir{i}/python{j}.jpg","wb")
        #r.content获取响应内容（字节流）
        f.write(r.content)
        f.close()
